package com.southsmart.sgeocserver.config;

import lombok.Data;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* 批量注入数据源配置类
* @author cjunxian
* @date 2021/7/30
*/
@Component(value = SqlDBConfig.BEAN_NAME)
@ConfigurationProperties(prefix = "sqldb")
@Data
public class SqlDBConfig {

    public static final String BEAN_NAME = "SqliteDBConfig";

    private List<SqlDB> datasource;

    /**
     * 根据配置文件中的key名获取sql配置
     * @param key
     * @return
     */
    public SqlDB getSqlDB(String key) {
        if(CollectionUtils.isEmpty(datasource)){
            return null;
        }
        Map<String, SqlDB> sqlDBMap =
                datasource.stream().collect(Collectors.toMap(SqlDB::getKey, Function.identity(),(key1, key2) -> key2));
        return sqlDBMap.get(key);
    }

}
